/* eslint-disable max-lines */
import type { ProposalQuoteDetail, ProposalForm } from '@/store/proposal'
import type { PPTTableElement, Slide, TableCell } from '@/types/slides'
import { nanoid } from 'nanoid'

const TABLE_WIDTH = 900
const QUOTE_FIXED_KEY = {
  单位: 'quoteUnit',
  单价: 'quotePrice',
  数量: 'quoteCount',
  税率: 'quoteRate',
  不含税金额: 'quoteAmount',
  备注: 'quoteDesc',

  小计: 'quoteSubtotal',
  小计备注: 'quoteSubtotalDesc',
  '增值税税金(6%)': 'quoteVat6',
  '增值税税金(6%)备注': 'quoteVat6Desc',
  '增值税税金(13%)': 'quoteVat13',
  '增值税税金(13%)备注': 'quoteVat13Desc',
  含税金额: 'quoteTotal',
  含税金额备注: 'quoteTotalDesc',
} as const

type FixedKeyType = keyof typeof QUOTE_FIXED_KEY

function getQuoteFixedKey(
  key: '单位' | '单价' | '数量' | '税率' | '不含税金额' | '备注',
  index: number,
): string

function getQuoteFixedKey(
  key:
    | '小计'
    | '小计备注'
    | '增值税税金(6%)'
    | '增值税税金(6%)备注'
    | '增值税税金(13%)'
    | '增值税税金(13%)备注'
    | '含税金额'
    | '含税金额备注',
): string

function getQuoteFixedKey(key: FixedKeyType, index?: number): string {
  return `${QUOTE_FIXED_KEY[key]}${index ?? ''}`
}

const headerText = [
  '序号',
  '智能化开发描述',
  '单位',
  '单价',
  '数量',
  '税率',
  '不含税金额',
  '备注',
] as const
type HeaderTextKeys = (typeof headerText)[number]

type DynamicData = {
  [key in HeaderTextKeys]?: string
}

const tableConfig = (): PPTTableElement => {
  return {
    type: 'table',
    id: nanoid(10),
    width: TABLE_WIDTH,
    height: 325,
    colWidths: [0.066, 0.215, 0.127, 0.126, 0.112, 0.104, 0.121, 0.127],
    rotate: 0,
    left: 0,
    top: 0,
    outline: { width: 1, style: 'solid', color: '#000000' },
    cellMinHeight: 36,
    data: [],
  }
}

const tableCell = (
  text?: string,
  config?: {
    bold?: boolean
    colspan?: number
    rowspan?: number
    align?: 'left' | 'center' | 'right'
  },
): TableCell => {
  return {
    id: nanoid(10),
    colspan: config?.colspan || 1,
    rowspan: config?.rowspan || 1,
    text: `{{${text}}}` || '',
    style: {
      fontname: 'Microsoft Yahei',
      color: '#333',
      align: config?.align || 'center',
      bold: config?.bold || false,
    },
  }
}

const tableHead = (): TableCell[] => {
  const cells = headerText.map((text) => tableCell(text, { bold: true }))
  return cells
}

const tableEmptyRow = (): TableCell[] => {
  const cells: TableCell[] = []
  cells.push(tableCell('', { colspan: headerText.length }))
  return cells
}

const tableDynamicRow = (data: DynamicData, index: number): TableCell[] => {
  const cells: TableCell[] = []
  for (const text of headerText) {
    switch (text) {
      case '序号':
      case '智能化开发描述':
        cells.push(tableCell(data[text]))
        break
      default:
        cells.push(tableCell(getQuoteFixedKey(text, index)))
        break
    }
  }

  return cells
}

const tableFootRow = (
  text: '小计' | '增值税税金(6%)' | '增值税税金(13%)' | '含税金额',
): TableCell[] => {
  const COL_SPAN = 6

  const cells: TableCell[] = []
  cells.push(tableCell(text, { colspan: COL_SPAN, align: 'left' }))

  Array.from({ length: COL_SPAN - cells.length }).forEach(() => {
    cells.push(tableCell())
  })

  cells.push(tableCell(getQuoteFixedKey(text)))
  cells.push(
    tableCell(
      getQuoteFixedKey(
        (text + '备注') as
          | '小计备注'
          | '增值税税金(6%)备注'
          | '增值税税金(13%)备注'
          | '含税金额备注',
      ),
    ),
  )

  return cells
}

/** 软件 + 硬件 + 施工差旅 */
const tableElementWithAll = ({
  left,
  top,
}: {
  left: number
  top: number
}): PPTTableElement => {
  const table = tableConfig()
  table.left = left - TABLE_WIDTH / 2
  table.top = top / 2

  table.data.push(tableHead())

  table.data.push(tableDynamicRow({ 序号: '1', 智能化开发描述: '软件报价' }, 0))
  table.data.push(tableDynamicRow({ 序号: '2', 智能化开发描述: '硬件报价' }, 1))
  table.data.push(tableDynamicRow({ 序号: '3', 智能化开发描述: '施工差旅' }, 2))

  table.data.push(tableEmptyRow())

  table.data.push(tableFootRow('小计'))
  table.data.push(tableFootRow('增值税税金(6%)'))
  table.data.push(tableFootRow('增值税税金(13%)'))
  table.data.push(tableFootRow('含税金额'))

  return table
}

/** 软件 + 硬件 */
const tableElementWithSoftEquip = ({
  left,
  top,
}: {
  left: number
  top: number
}): PPTTableElement => {
  const table = tableConfig()
  table.left = left - TABLE_WIDTH / 2
  table.top = top / 2

  table.data.push(tableHead())

  table.data.push(tableDynamicRow({ 序号: '1', 智能化开发描述: '软件报价' }, 0))
  table.data.push(tableDynamicRow({ 序号: '2', 智能化开发描述: '硬件报价' }, 1))

  table.data.push(tableEmptyRow())

  table.data.push(tableFootRow('小计'))
  table.data.push(tableFootRow('增值税税金(6%)'))
  table.data.push(tableFootRow('增值税税金(13%)'))
  table.data.push(tableFootRow('含税金额'))

  return table
}

/** 硬件 + 施工差旅 */
const tableElementWithEquipHardTravel = ({
  left,
  top,
}: {
  left: number
  top: number
}): PPTTableElement => {
  const table = tableConfig()
  table.left = left - TABLE_WIDTH / 2
  table.top = top / 2

  table.data.push(tableHead())

  table.data.push(tableDynamicRow({ 序号: '1', 智能化开发描述: '硬件报价' }, 0))
  table.data.push(tableDynamicRow({ 序号: '2', 智能化开发描述: '施工差旅' }, 1))

  table.data.push(tableEmptyRow())

  table.data.push(tableFootRow('小计'))
  table.data.push(tableFootRow('增值税税金(6%)'))
  table.data.push(tableFootRow('增值税税金(13%)'))
  table.data.push(tableFootRow('含税金额'))

  return table
}

const getFootPage = (quote: ProposalQuoteDetail): Slide => {
  const slide: Slide = {
    id: nanoid(10),
    elements: [
      {
        type: 'table',
        id: nanoid(10),
        width: 908.72705849263,
        height: 461.1875,
        colWidths: [
          0.07535088687245296, 0.30578802884948497, 0.07790037236849999,
          0.1330423172942144, 0.10813036761597758, 0.1498940134996851,
          0.1498940134996851,
        ],
        rotate: 0,
        data: [
          [
            {
              id: nanoid(10),
              colspan: 7,
              rowspan: 1,
              text: `${quote.projectName || ''}报价总表`,
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
                align: 'center',
                fontsize: '12px',
              },
            },
            {
              id: nanoid(10),
              colspan: 1,
              rowspan: 1,
              text: '',
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
              },
            },
            {
              id: nanoid(10),
              colspan: 1,
              rowspan: 1,
              text: '',
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
              },
            },
            {
              id: nanoid(10),
              colspan: 1,
              rowspan: 1,
              text: '',
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
              },
            },
            {
              id: nanoid(10),
              colspan: 1,
              rowspan: 1,
              text: '',
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
              },
            },
            {
              id: nanoid(10),
              colspan: 1,
              rowspan: 1,
              text: '',
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
              },
            },
            {
              id: nanoid(10),
              colspan: 1,
              rowspan: 1,
              text: '',
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
              },
            },
          ],
          [
            {
              id: nanoid(10),
              colspan: 7,
              rowspan: 1,
              text: `客户信息\n公司名：\n地址：${quote.projectAddress || ''}\n电话：<div>联系人：${quote.superintendent || ''}\n\n报价单位\n</div><div>公司名：${quote.deptName || ''}\n</div><div>地址：${quote.deptAddress || ''}\n</div><div>电话：${quote.phoneNumber || ''}\n</div><div>联系人：${quote.userNickName || ''}</div>`,
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
                align: 'left',
                fontsize: '12px',
              },
            },
            {
              id: nanoid(10),
              colspan: 1,
              rowspan: 1,
              text: '',
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
                fontsize: '12px',
              },
            },
            {
              id: nanoid(10),
              colspan: 1,
              rowspan: 1,
              text: '',
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
                fontsize: '12px',
              },
            },
            {
              id: nanoid(10),
              colspan: 1,
              rowspan: 1,
              text: '',
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
                fontsize: '12px',
              },
            },
            {
              id: nanoid(10),
              colspan: 1,
              rowspan: 1,
              text: '',
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
                fontsize: '12px',
              },
            },
            {
              id: nanoid(10),
              colspan: 1,
              rowspan: 1,
              text: '',
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
                fontsize: '12px',
              },
            },
            {
              id: nanoid(10),
              colspan: 1,
              rowspan: 1,
              text: '',
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
                fontsize: '12px',
              },
            },
          ],
          [
            {
              id: nanoid(10),
              colspan: 1,
              rowspan: 1,
              text: '序号',
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
                fontsize: '12px',
              },
            },
            {
              id: nanoid(10),
              colspan: 1,
              rowspan: 1,
              text: '智能化开发描述',
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
                fontsize: '12px',
              },
            },
            {
              id: nanoid(10),
              colspan: 1,
              rowspan: 1,
              text: '单位',
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
                fontsize: '12px',
              },
            },
            {
              id: nanoid(10),
              colspan: 1,
              rowspan: 1,
              text: '单价',
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
                fontsize: '12px',
              },
            },
            {
              id: nanoid(10),
              colspan: 1,
              rowspan: 1,
              text: '数量',
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
                fontsize: '12px',
              },
            },
            {
              id: nanoid(10),
              colspan: 1,
              rowspan: 1,
              text: '金额',
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
                fontsize: '12px',
              },
            },
            {
              id: nanoid(10),
              colspan: 1,
              rowspan: 1,
              text: '备注',
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
                fontsize: '12px',
              },
            },
          ],
          [
            {
              id: nanoid(10),
              colspan: 1,
              rowspan: 1,
              text: '1',
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
                fontsize: '12px',
              },
            },
            {
              id: nanoid(10),
              colspan: 1,
              rowspan: 1,
              text: '软件报价',
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
                fontsize: '12px',
              },
            },
            {
              id: nanoid(10),
              colspan: 1,
              rowspan: 1,
              text: '项',
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
                fontsize: '12px',
              },
            },
            {
              id: nanoid(10),
              colspan: 1,
              rowspan: 1,
              text: quote.funcPriceStr || '',
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
                fontsize: '12px',
              },
            },
            {
              id: nanoid(10),
              colspan: 1,
              rowspan: 1,
              text: '1',
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
                fontsize: '12px',
              },
            },
            {
              id: nanoid(10),
              colspan: 1,
              rowspan: 1,
              text: quote.funcPrice + '',
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
                fontsize: '12px',
              },
            },
            {
              id: nanoid(10),
              colspan: 1,
              rowspan: 1,
              text: '',
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
                fontsize: '12px',
              },
            },
          ],
          [
            {
              id: nanoid(10),
              colspan: 1,
              rowspan: 1,
              text: '2',
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
                fontsize: '12px',
              },
            },
            {
              id: nanoid(10),
              colspan: 1,
              rowspan: 1,
              text: '硬件报价',
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
                fontsize: '12px',
              },
            },
            {
              id: nanoid(10),
              colspan: 1,
              rowspan: 1,
              text: '项',
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
                fontsize: '12px',
              },
            },
            {
              id: nanoid(10),
              colspan: 1,
              rowspan: 1,
              text: quote.eqptPriceStr || '',
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
                fontsize: '12px',
              },
            },
            {
              id: nanoid(10),
              colspan: 1,
              rowspan: 1,
              text: '1',
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
                fontsize: '12px',
              },
            },
            {
              id: nanoid(10),
              colspan: 1,
              rowspan: 1,
              text: quote.eqptPrice + '',
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
                fontsize: '12px',
              },
            },
            {
              id: nanoid(10),
              colspan: 1,
              rowspan: 1,
              text: '',
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
                fontsize: '12px',
              },
            },
          ],
          [
            {
              id: nanoid(10),
              colspan: 1,
              rowspan: 1,
              text: '3',
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
                fontsize: '12px',
              },
            },
            {
              id: nanoid(10),
              colspan: 1,
              rowspan: 1,
              text: '施工差旅报价',
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
                fontsize: '12px',
              },
            },
            {
              id: nanoid(10),
              colspan: 1,
              rowspan: 1,
              text: '项',
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
                fontsize: '12px',
              },
            },
            {
              id: nanoid(10),
              colspan: 1,
              rowspan: 1,
              text: quote.travelPriceStr || '',
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
                fontsize: '12px',
              },
            },
            {
              id: nanoid(10),
              colspan: 1,
              rowspan: 1,
              text: '1',
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
                fontsize: '12px',
              },
            },
            {
              id: nanoid(10),
              colspan: 1,
              rowspan: 1,
              text: quote.travelPrice + '',
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
                fontsize: '12px',
              },
            },
            {
              id: nanoid(10),
              colspan: 1,
              rowspan: 1,
              text: '',
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
                fontsize: '12px',
              },
            },
          ],
          [
            {
              id: nanoid(10),
              colspan: 7,
              rowspan: 1,
              text: '<br>',
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
              },
            },
            {
              id: nanoid(10),
              colspan: 1,
              rowspan: 1,
              text: '',
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
              },
            },
            {
              id: nanoid(10),
              colspan: 1,
              rowspan: 1,
              text: '',
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
              },
            },
            {
              id: nanoid(10),
              colspan: 1,
              rowspan: 1,
              text: '',
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
              },
            },
            {
              id: nanoid(10),
              colspan: 1,
              rowspan: 1,
              text: '',
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
              },
            },
            {
              id: nanoid(10),
              colspan: 1,
              rowspan: 1,
              text: '',
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
              },
            },
            {
              id: nanoid(10),
              colspan: 1,
              rowspan: 1,
              text: '',
              style: {
                fontname: 'Microsoft Yahei',
                color: '#333',
              },
            },
          ],
          [
            {
              colspan: 5,
              rowspan: 1,
              text: '合计金额',
              id: nanoid(10),
              style: {
                align: 'right',
              },
            },
            {
              colspan: 1,
              rowspan: 1,
              text: '',
              id: nanoid(10),
            },
            {
              colspan: 1,
              rowspan: 1,
              text: '',
              id: nanoid(10),
            },
            {
              colspan: 1,
              rowspan: 1,
              text: '',
              id: nanoid(10),
            },
            {
              colspan: 1,
              rowspan: 1,
              text: '合计金额',
              id: nanoid(10),
            },
            {
              colspan: 1,
              rowspan: 1,
              text: quote.allTotalStr + '',
              id: nanoid(10),
            },
            {
              colspan: 1,
              rowspan: 1,
              text: '',
              id: nanoid(10),
            },
          ],
        ],
        left: 52.10942613467739,
        top: 55.940089788934955,
        outline: {
          width: 1,
          style: 'solid',
          color: '#000000',
        },
        cellMinHeight: 36,
      },
      {
        type: 'text',
        id: nanoid(10),
        left: 52.10942613467739,
        top: 509.8475960432482,
        width: 600,
        height: 40,
        content:
          '<p style=""><span style="font-size: 10px;">以上报价仅供参考，作为双方确认合作意向之用，未经授权请勿透露给任何第三方，具体金额以正式的报价为准。</span></p>',
        rotate: 0,
        defaultFontName: 'Microsoft Yahei',
        defaultColor: '#333',
        vertical: false,
        lineHeight: 1,
      },
    ],
    background: { type: 'solid', color: '#fff' },
  }
  return slide
}

export {
  QUOTE_FIXED_KEY,
  getQuoteFixedKey,
  tableElementWithAll,
  tableElementWithSoftEquip,
  tableElementWithEquipHardTravel,
  getFootPage,
}
